package com.laicunba.util;

import java.io.File;
import java.io.OutputStream;
import java.util.Hashtable;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

public class QRcodeUtil {

	public static void main(String[] args) throws Exception {

		File file = new File("d:/data/new2.gif");
		QRcodeUtil.make("http://www.u1hd.com/p/269ea04b-d2ae-4d1d-b1de-8e639afa4a89.htm", file, 300, 300);

	}

	public static void make(String content, File file, int width, int height) throws Exception {
		String format = "gif";// 二维码的图片格式
		Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
		hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
		BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
		MatrixToImageWriter.writeToFile(bitMatrix, format, file);
	}

	public static void make(String content, OutputStream outputStream, int width, int height) throws Exception {
		String format = "gif";// 二维码的图片格式
		Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
		hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
		BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
		MatrixToImageWriter.writeToStream(bitMatrix, format, outputStream);
	}

}
